//
// Templet.h
//
// $Id: //base/include/Templet.h
//
// Library: base
// Package: base
// Module : Templet
// Author :	Jelin,doitsjz@163.com
// Date	  :	10/30/2012
// 
// Definition of the Templet class.
//
// Copyright (c) 2012-2012, iGuard.
//
#include "RTSPServerSession.h"
#include <sstream>

int RTSPServerSession::handleMessage(RTSPRequest& request)
{
	string method = request.getMethod();

	if (method == RTSPRequest::OPTIONS)
		OnOptionsRequest(request);
	else if (method == RTSPRequest::DESCRIBE)
		OnDescribeRequest(request);
	else if (method == RTSPRequest::SETUP)
		OnSetupRequest(request);
	else if (method == RTSPRequest::PLAY)
		OnPlayRequest(request);
	else if (method == RTSPRequest::PAUSE)
		OnPauseRequest(request);
	else if (method == RTSPRequest::RECORD)
		OnRecordRequest(request);
	else if (method == RTSPRequest::TEARDOWN)
		OnTeardownRequest(request);
	else
		;//default nothing ,future add err deal or selfdefine method
	return true;
}
int RTSPServerSession::handleMessage(RTSPResponse& reply)
{
	return 0;
}
int RTSPServerSession::makeDescribeReply(int status,string &sdp,RTSPRequest& request,string& reply)
{
	RTSPResponse rep((RTSPResponse::RTSPStatus)status);
	rep.setCSeq(request.getCSeq());
	rep.setDate();
	rep.setUserAgent();
	if (!sdp.empty())
	{
		rep.setContentType(RTSPMessage::APP_SDP_TYPE);
		rep.setContentBody(sdp);
		rep.setContentLength(sdp.length());
	}

	std::ostringstream ostr;
	rep.write(ostr);
	reply = ostr.str();

	return 0;
}
int RTSPServerSession::makeSetupReply(int status,int port,RTSPRequest& request,string& reply)
{
	RTSPResponse rep((RTSPResponse::RTSPStatus)status);
	rep.setCSeq(request.getCSeq());
	rep.setDate();
	rep.setUserAgent();
	rep.setSession(generateUUID());

	CRtspTransport Transport;
	request.getTransport(Transport);
	Transport.setServerPort(port);
	rep.setTransport(Transport);

	std::ostringstream ostr;
	rep.write(ostr);
	reply = ostr.str();

	return 0;
}
int RTSPServerSession::makePlayReply(int status,RTSPRequest& request,string& reply)
{
	RTSPResponse rep((RTSPResponse::RTSPStatus)status);
	rep.setCSeq(request.getCSeq());
	rep.setDate();
	rep.setUserAgent();
	rep.setSession(request.getSession());

	std::ostringstream ostr;
	rep.write(ostr);
	reply = ostr.str();

	return 0;
}
int RTSPServerSession::makePauseReply(int status,RTSPRequest& request,string& reply)
{
	RTSPResponse rep((RTSPResponse::RTSPStatus)status);
	rep.setCSeq(request.getCSeq());
	rep.setDate();
	rep.setUserAgent();
	rep.setSession(request.getSession());

	std::ostringstream ostr;
	rep.write(ostr);
	reply = ostr.str();

	return 0;
}
int RTSPServerSession::makeRecordReply(int status,RTSPRequest& request,string& reply)
{
	RTSPResponse rep((RTSPResponse::RTSPStatus)status);
	rep.setCSeq(request.getCSeq());
	rep.setDate();
	rep.setUserAgent();
	rep.setSession(request.getSession());

	std::ostringstream ostr;
	rep.write(ostr);
	reply = ostr.str();

	return 0;
}
int RTSPServerSession::makeTeardownReply(int status,RTSPRequest& request,string& reply)
{
	RTSPResponse rep((RTSPResponse::RTSPStatus)status);
	rep.setCSeq(request.getCSeq());
	rep.setDate();
	rep.setUserAgent();
	rep.setSession(request.getSession());

	std::ostringstream ostr;
	rep.write(ostr);
	reply = ostr.str();

	return 0;
}
